﻿namespace Pibby.Core.IO
{
	using System;
	using System.IO;

	public class FileSystemFile : IFile, IEquatable<IFile>
	{
		public FileSystemFile(string path)
		{
			this.Info = new FileInfo(path);
		}

		#region Properties

		public string Name
		{
			get { return System.IO.Path.GetFileName(this.Path); }
		}

		public string NameNoExtension
		{
			get { return System.IO.Path.GetFileNameWithoutExtension(this.Path); }
		}

		public string FolderName
		{
			get { return System.IO.Path.GetDirectoryName(this.Path); }
		}

		public string Path
		{
			get { return this.Info.FullName; }
		}

		public string Extension
		{
			get { return System.IO.Path.GetExtension(this.Path); }
		}

		public long Length
		{
			get { return this.Info.Length; }
		}

		public bool Exists
		{
			get { return System.IO.File.Exists(this.Path); }
		}

		public DateTime CreationTime
		{
			get
			{
				FileInfo fi = new FileInfo(this.Path);

				return fi.CreationTime;
			}
		}

		public DateTime LastAccessTime
		{
			get
			{
				FileInfo fi = new FileInfo(this.Path);

				return fi.LastAccessTime;
			}
		}

		public DateTime LastWriteTime
		{
			get
			{
				FileInfo fi = new FileInfo(this.Path);

				return fi.LastWriteTime;
			}
		}

		private FileInfo Info 
		{ 
			get; 
			set; 
		}

		#endregion Properties

		#region Methods

		#region ToString

		public override string ToString()
		{
			return string.Format(
						"{0} : {1}", 
						this.Name,
						FileSizeInterpreter.InterpretSize(this.Length));
		}

		#endregion

		#region IEquatable<IFile>

		public bool Equals(IFile other)
		{
			return this.Name.Equals(other.Name, StringComparison.CurrentCultureIgnoreCase)
				&& this.Length == other.Length;
		}

		public override int GetHashCode()
		{
			return this.Name.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null)
			{
				return base.Equals(obj);
			}

			IFile item = obj as IFile;

			if (item == null)
			{
				return false;
			}

			return this.Equals(item);
		}

		#endregion

		public void Delete()
		{
			System.IO.File.Delete(this.Path);
		}

		#endregion Methods
	}
}
